# --------- import和get_asgi_application()的位置顺序不能变!
import os
from django.core.asgi import get_asgi_application

os.environ.setdefault('DJANGO_SETTINGS_MODULE', 'websocket_my.settings')
django_asgi_app = get_asgi_application()
# ------- 必须在最前面, 不然部署的时候报错!

from channels.auth import AuthMiddlewareStack
from channels.routing import ProtocolTypeRouter, URLRouter
import chat.routing

from django.conf.urls import url
from chat.consumers import ChatConsumer
from django.conf.urls import re_path
from django.urls import path


# application = ProtocolTypeRouter({
#   "http": django_asgi_app,
#
#   # WebSocket chat handler
#   "websocket": AuthMiddlewareStack(
#         URLRouter([
#             path('ws/table/<slug:table_id>/', ChatConsumer),
#             url(r"^chat/$", ChatConsumer.as_asgi()),
#             # chat.routing.websocket_urlpatterns,
#         ])
#     ),
# })


application = ProtocolTypeRouter({
  "http": django_asgi_app,

  # WebSocket chat handler
  "websocket": AuthMiddlewareStack(
        URLRouter(
            chat.routing.websocket_urlpatterns,
        )
    ),
})

# print("----------")
# print(chat.routing.websocket_urlpatterns)